package com.huafeng.function;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.lang.UUID;
import com.huafeng.config.BotPropertiesConfig;
import com.huafeng.config.ScheduledConfig;
import com.huafeng.entity.GroupProperties;
import com.huafeng.utils.MessageUtils;
import com.huafeng.utils.SpringContextUtils;
import lombok.extern.slf4j.Slf4j;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author 花风
 * @date 2023/4/6 12:51
 * @Description 通知处理类
 */
@Slf4j
public class NoticeFunctions {

    private static final BotPropertiesConfig botProperties = SpringContextUtils.getBean(BotPropertiesConfig.class);

    public static final Map<String, GroupProperties> map = botProperties.getManage_groups();

    public static final Boolean IS_SAVE = botProperties.getIs_save();

    public static Object groupDecrease(List<String> params){
        //获取参数
        String userId = params.get(1);
        String groupId = params.get(2);

        Object result = null;
        if (map.containsKey(groupId)){
            //生成内容
            String content = "有人离开了\n账号为：" + userId;
            //向群里发送消息
            result = MessageUtils.sendMessage(content,groupId,"group");
        }
        return result;
    }

    public static Object groupIncrease(List<String> params){
        //获取参数
        String userId = params.get(1);
        String groupId = params.get(2);

        Object result = null;
        if (map.containsKey(groupId)){
            //生成内容
            String content = "[CQ:at,qq="+ userId + "]欢迎加入群聊!\n请及时修改备注!";
            //向群里发送消息
            result = MessageUtils.sendMessage(content,groupId,"group");
        }
        return result;
    }

    public static Object friendAdd(List<String> params){
        //获取参数
        String userId = params.get(1);
        //生成内容
        String content = "你好呀";
        return MessageUtils.sendMessage(content,userId,"private");
    }

    public static Object groupRecall(List<String> params){
        String messageId = params.get(0);
        String groupId = params.get(2);
        //根据消息id获取消息内容
        String message = "群号为：" + groupId + "\n";
        message += MessageUtils.getMessage(messageId);
        if (IS_SAVE){
            ScheduledConfig.messageList.add(message);
        }
        return "ok";
    }

    public static Object friendRecall(List<String> params){
        String messageId = params.get(0);
        //根据消息ID获取消息
        String message = MessageUtils.getMessage(messageId);

        if (IS_SAVE){
            ScheduledConfig.messageList.add(message);
        }
        return null;
    }

    public static Object Notify(List<String> params){
        //获取参数
        String userId = params.get(1);
        String groupId = params.get(2);

        String content = "";
        Object result = null;
        if (groupId != null){
            //如果是群内戳一戳
            content += "[CQ:at,qq=" + userId + "]晓梦在此，有何贵干";
            result = MessageUtils.sendMessage(content,groupId,"group");
        }else {
            //如果是好友戳一戳
            content += "晓梦在此，有何贵干";
            result = MessageUtils.sendMessage(content,userId,"private");
        }
        return result;
    }

}
